package com.xhsj.user.pms.patent.controller;



import com.xhsj.user.pms.patent.service.DisclosureService;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: liuqinghua
 * @Description:报表
 * @Data:2020/3/11
 */
@Api(value = "报表", description = "报表")
@RestController
@RequestMapping("/report")
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
public class ReportController {
    @Autowired
    private DisclosureService disclosureService;

    /**
     * 报表饼状图年月季
     *type  (0:月    1:季    2:年)
     * @param
     * @return
     */
    @ApiOperation(value = "报表饼状图年月季", notes = "报表饼状图年月季")
    @PostMapping("/reportPie1")
    public Message reportPie1(@RequestBody Map<String, Object> param) {
           try {
               Map<String, Object> paramMap = new HashMap<>();
               log.info("报表饼状图年月季参数{}",param);
               if(StringUtils.isNull(param.get("type")))
                   return Message.error(1,"区分是年月季不能为空");
               if(StringUtils.isNull(param.get("typeParam")))
                   return Message.error(1,"区分哪年哪月哪季不能为空");
               String  typeParam= String.valueOf(param.get("typeParam"));
               int  type= Integer.valueOf(String.valueOf(param.get("type")));
               List<Map<String, Object>> numData =   disclosureService.getNumData(typeParam,type);
               paramMap.put("type",type);
               paramMap.put("typeParam",typeParam);
               numData.add(paramMap);
               return Message.success("报表饼状图年月季成功",numData);
          } catch (Exception e) {
               log.error("报表饼状图年月季失败",e);
               e.printStackTrace();
               return  Message.error("报表饼状图年月季失败!");
           }
    }

    @ApiOperation(value = "报表饼状图", notes = "报表饼状图")
    @PostMapping("/reportPie")
    public Message reportPie(@RequestBody Map<String,Object> param) {
        try {
            log.info("报表饼状图参数{}",param);
            List<Map<String, Object>> numData = disclosureService.getPieNumData(param);
            return Message.success("报表饼状图成功",numData);
        } catch (Exception e) {
            log.error("报表饼状图失败",e);
            e.printStackTrace();
            return  Message.error("报表饼状图失败!");
        }
    }

    /**
     * 报表柱状图个人
     *
     * @param
     * @return
     */
    @ApiOperation(value = "报表柱状图个人重要普通", notes = "报表柱状图个人重要普通")
    @PostMapping("/reportCategory")
    public Message reportCategory(@RequestBody Map<String, Object> param) {
        try {
            log.info("报表柱状图个人重要普通参数{}",param);
            if(StringUtils.isNull(param.get("type")))
                return Message.error(1,"区分是年月季不能为空");
            if(StringUtils.isNull(param.get("typeKinds")))
                return Message.error(1,"区分类型参数不能为空");
            if(StringUtils.isNull(param.get("typeParam")))
                return Message.error(1,"区分哪年哪月哪季不能为空");
            String  typeParam= String.valueOf(param.get("typeParam"));
            String typeKinds=  String.valueOf(param.get("typeKinds"));
            int type= Integer.valueOf(String.valueOf(param.get("type")));
            List<Map<String, Object>> numData = disclosureService.getNumDataByUser(typeParam,typeKinds,type);
            List<Map<String, Object>> listNum =new ArrayList<>();
            Map<String, Object> map =new HashMap<>();
            for (Map<String, Object> data : numData) {
                data.put("type",type);
                data.put("typeParam",typeParam);
                data.put("typeKinds",typeKinds);
                String name = String.valueOf(data.get("name"));
                String significanceIdentify = String.valueOf(data.get("significanceIdentify"));
                BigDecimal value = new BigDecimal(String.valueOf(data.get("value")));
                if(map.containsKey(name)&&"1".equals(significanceIdentify)){
                    Map<String,Object> oValue = (Map<String, Object>) map.get(name);
                    oValue.put("value1",value);
                    map.put(name,oValue);
                }else if(map.containsKey(name)&&("2".equals(significanceIdentify) || "0".equals(significanceIdentify))){
                    Map<String,Object> oValue = (Map<String, Object>) map.get(name);
                    oValue.put("value", new BigDecimal(String.valueOf(oValue.get("value"))).add(value));
                    map.put(name,oValue);
                }else if("1".equals(significanceIdentify)){
                    data.put("value1",value);
                    data.put("significanceIdentify1",Integer.valueOf(significanceIdentify));
                    data.put("value",0);
                    data.put("significanceIdentify",2);
                    map.put(name,data);
                }else if("2".equals(significanceIdentify) || "0".equals(significanceIdentify)){
                    data.put("value1",0);
                    data.put("significanceIdentify1",1);
                    data.put("significanceIdentify",2);
                    map.put(name,data);
                }
            }

            for (Map.Entry<String, Object> m : map.entrySet()) {
                Map<String, Object> value = (Map<String, Object>) m.getValue();
                listNum.add(value);
            }

            return Message.success("报表柱状图个人重要普通成功",listNum);
        } catch (Exception e) {
            log.error("报表柱状图个人重要普通失败",e);
            e.printStackTrace();
            return  Message.error("报表柱状图个人重要普通失败!");
        }
    }
    /**
     * 交底书所有数据总数
     *
     * @param
     * @return
     */
    @ApiOperation(value = "交底书所有数据总数", notes = "交底书所有数据总数")
    @GetMapping("/disclosureTotal")
    public Message disclosureTotal() {
        try {
            log.info("获取交底书所有数据总数");
            int numData = disclosureService.getDisclosureTotal();
            return Message.success("获取数据成功",numData);
        } catch (Exception e) {
            log.error("获取数据失败",e);
            e.printStackTrace();
            return  Message.error("获取数据失败!");
        }
    }

    @ApiOperation(value = "交底书所有数据总数", notes = "交底书所有数据总数")
    @PostMapping("/disclosureTotalByType")
    public Message disclosureTotalByType(@RequestBody Map<String, Object> param) {
        try {
            log.info("获取交底书所有数据总数");
            String type = String.valueOf(param.get("type"));
            String typeParam = String.valueOf(param.get("typeParam"));
            String typeKinds = String.valueOf(param.get("typeKinds"));
            Integer numData = disclosureService.disclosureTotalByType(type,typeParam,typeKinds);
            return Message.success("获取数据成功",numData);
        } catch (Exception e) {
            log.error("获取数据失败",e);
            e.printStackTrace();
            return  Message.error("获取数据失败!");
        }
    }

    @ApiOperation(value = "报表状态", notes = "报表状态")
    @GetMapping("/getPhaseStatus")
    public Message getPhaseStatus() {
        try {
            return Message.success("报表状态成功",disclosureService.getPhaseStatus());
        }catch (Exception e){
            log.error("报表状态异常信息:{}",e);
            e.printStackTrace();
            return Message.error(1,"报表状态失败");
        }
    }

}
